package blockchain

import (
	"context"
	"time"

	"private-domain-overseas-service/app/business/api/internal/repository/blockchain"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/app/business/api/internal/types"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzblockchainwalletrecord"
	"private-domain-overseas-service/tools/ent/predicate"

	"github.com/samber/lo"
	"github.com/shopspring/decimal"
	"github.com/zeromicro/go-zero/core/logx"
)

type BlockchainWalletRecordsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 区块链钱包记录
func NewBlockchainWalletRecordsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BlockchainWalletRecordsLogic {
	return &BlockchainWalletRecordsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BlockchainWalletRecordsLogic) BlockchainWalletRecords(req *types.BlockchainWalletRecordsRequest) (resp *types.BlockchainWalletRecordsResponse, err error) {
	blockchainWalletRecordDao := blockchain.NewBlockchainWalletRecordDao(l.ctx, l.svcCtx)

	// 获取用户ID
	userID := common.JwtAuthUserId(l.ctx)
	// 百分之一
	baiDecimal := decimal.NewFromInt32(100)
	// 千分之一
	thousandDecimal := decimal.NewFromInt32(100)

	// 构造查询条件
	walletRecordConditions := make([]predicate.HzBlockchainWalletRecord, 0)
	// 用户条件
	walletRecordConditions = append(walletRecordConditions, hzblockchainwalletrecord.UserIDEQ(userID))
	// 类型条件
	if req.Type != 0 && req.Type != 4 {
		walletRecordConditions = append(walletRecordConditions, hzblockchainwalletrecord.RecordStatusEQ(req.Type))
	}
	//最近30天
	if req.Type == 4 {
		now := time.Now()
		strat := common.GetRecentThirtyDays(now)
		walletRecordConditions = append(walletRecordConditions, hzblockchainwalletrecord.CreatedAtGTE(strat.Unix()))
		walletRecordConditions = append(walletRecordConditions, hzblockchainwalletrecord.CreatedAtLTE(now.Unix()))
	}
	// 查询钱包记录
	blockchainWalletList, total, err := blockchainWalletRecordDao.BlockchainWalletRecordConditionsPageAll(walletRecordConditions, req.Current, req.PageSize)
	if err != nil || total == 0 {
		return resp, nil
	}

	// 处理数据
	list := lo.Map(blockchainWalletList, func(record *ent.HzBlockchainWalletRecord, _ int) *types.RecordsList {
		item := &types.RecordsList{
			RecordTitle:                record.RecordTitle,
			RecordNumber:               record.RecordNumber,
			RecordHash:                 record.RecordHash,
			RecordIntegralAmount:       decimal.NewFromFloat(float64(record.RecordIntegralAmount)).Div(baiDecimal).StringFixed(2),
			RecordHzbCoin:              decimal.NewFromFloat(float64(record.RecordHzbCoin)).Div(baiDecimal).StringFixed(2),
			RecordRates:                decimal.NewFromFloat(float64(record.RecordRates)).Div(thousandDecimal).StringFixed(2),
			RecordServiceCharge:        decimal.NewFromFloat(float64(record.RecordServiceCharge)).Div(baiDecimal).StringFixed(2),
			RecordStatus:               record.RecordStatus,
			RecordRejectCause:          record.RecordRejectCause,
			RecordWalletAddress:        record.RecordWalletAddress,
			RecordTransferStatus:       record.RecordTransferStatus,
			RecordTransferCompleteTime: record.RecordTransferCompleteTime,
			RecordRejectTime:           record.RecordRejectTime,
			RecordArrivedTime:          record.RecordArrivedTime,
			RecordPassTime:             record.RecordPassTime,
			CreatedAt:                  record.CreatedAt,
		}
		return item
	})

	// 返回数据
	resp = &types.BlockchainWalletRecordsResponse{
		Page: &types.PageResponse{
			Current:  req.Current,
			PageSize: req.PageSize,
			Total:    int64(total),
		},
		List: list,
	}

	return resp, nil
}
